package day3;
//浅克隆 深克隆
public class Demo2 {
    public static void main(String[] args) throws CloneNotSupportedException {
//        A a=new A();
//        a.b=new B();
//        A a2=(A)a.clone();
//        System.out.println(a); //A@4554617c
//        System.out.println(a2); //A@74a14482
//        a.id=100;
//        a.name="张三";
//        //引用类型不会克隆
//        a.b.id=1000;
//        a.b.name="王五";
//        System.out.println(a.id+" "+a.name+" "+a.b+" "+a.b.id+" "+a.b.name);
//        System.out.println(a2.id+" "+a2.name+" "+a2.b+" "+a2.b.id+" "+a2.b.name);

        A a=new A();
        a.b=new B();
        a.b.c=new C();
        A a2=(A)a.clone();
        System.out.println(a+" "+a.b+" "+a.b.c);
        //结果如果 三个引用类型 地址都不同 表示深克隆成功
        System.out.println(a2+" "+a2.b+" "+a2.b.c);
    }
}
//深克隆
class A implements Cloneable{
    Integer id;
    String name;
    B b;
    protected Object clone() throws CloneNotSupportedException {
        A a=(A)super.clone();
        a.b=(B)b.clone();
        return a;
    }
}
class B implements Cloneable{
    Integer id;
    String name;
    C c;
    protected Object clone() throws CloneNotSupportedException {
        B b=(B)super.clone();
        b.c=(C)c.clone();
        return b;
    }
}
class C implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}